package org.mao.forumtest;

import java.io.File;
import java.io.FileWriter;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class ReadCar {

	public void read(String url) throws Exception {
		Document doc = Jsoup.connect(url).get();
		// Document doc = Jsoup.parse(new
		// File("D:\\carYaZhouYouMaZhuanTieQu.txt"),"UTF-8");

		File file = new File("D:\\temp.txt");
		FileWriter fw = new FileWriter(file);

		Elements eles = doc.getElementsContainingText("版块主题");
		Element table = null;
		for (Element ele : eles) {
			if (ele.tagName().toLowerCase().equals("table")) {
				table = ele;
				break;
			}
		}
		if (table != null) {
			Elements ths = table.select("tbody>tr>th");
			for (Element th : ths) {
				Elements links = th.select("a");
				if (links.size() > 1) {
					Element link = links.get(1);
					String href = link.attr("abs:href");
					String text = link.text();
					System.out.println(text + ":" + href + "\r\n");
				}
			}
		}

		// Element table = doc.getElementById("forum_58");
		// Elements trs = table.select("tbody>tr");
		// System.out.println(trs.size());
		// File file = new File("D:\\carYaZhouYouMaZhuanTieQuTemp.txt");
		// FileWriter fw = new FileWriter(file);
		fw.write(doc.html());
		fw.close();
		System.exit(-1);
	}

	public static void main(String[] arg) {
		try {
			ReadCar car = new ReadCar();
			car.read("http://38.103.161.184/bbs/forum-143-1.html");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
